﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MHYS.DEMO.BLL;
using MHYS.DEMO.Model;
namespace Demo
{
    public partial class Product : System.Web.UI.Page
    {


        protected static MHYS.DEMO.Model.Product product;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (this.Request.QueryString["PID"] != null)
                {
                    BindData(Convert.ToInt32(this.Request.QueryString["PID"].ToString()));
                }
                else
                {
                    this.Response.Redirect("ProductList.aspx");
                }
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        void BindData(int Pid)
        {
            product = ProductManager.ShowProduct(Pid).FirstOrDefault();
            this.DlProImg.DataSource = ProductManager.ShowProduct(Pid).FirstOrDefault().Image;
            this.DlProImg.DataBind();
            this.proImg.ImageUrl = "images/" + ProductManager.ShowProduct(Pid).FirstOrDefault().ImageUrl;
        }

        /// <summary>
        /// 购买商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnBuy_Click(object sender, EventArgs e)
        {
            if (this.Session["ShopCar"] == null)
            {
                List<OrderItem> orderItemList = new List<OrderItem>();
                OrderItem orderItem = new OrderItem()
                {
                    Product = product,
                    Num = Convert.ToInt32(this.buy_count.Value)
                };
                orderItemList.Add(orderItem);
                this.Session["ShopCar"] = orderItemList;
            }
            else
            {
                //将Session中的对象取出
                List<OrderItem> orderItemsList = this.Session["ShopCar"] as List<OrderItem>;

                //创建当前商品
                OrderItem orderItem = new OrderItem
                {
                    Num = Convert.ToInt32(this.buy_count.Value),
                    Product = product
                };
                //判断购物车中是否有此商品
                foreach (var item in orderItemsList)
                {
                    if (item.Product.GName == orderItem.Product.GName)
                    {
                        orderItemsList.Find(m => m.Product.GName == item.Product.GName).Num += orderItem.Num;

                        return;
                    }
                }

                //如果购物车中没有此商品，则添加
                orderItemsList.Add(orderItem);

                //转换Object添加到Session中
                this.Session["ShopCar"] = orderItemsList;
            }
        }

        protected void Timer1_PreRender(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 刷新购物车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            this.ProCount.InnerHtml = this.Session["ShopCar"] == null ? 0.ToString() : (this.Session["ShopCar"] as List<OrderItem>).Count.ToString();//显示购物车
        }


    }
}